clear 
clear matrix
clear mata
set mem 1000000
set maxvar 30000
set more off

use "${data_raw}\randhrs1992_2016v1.dta", clear
rename  hhidpn ID
set more off

* renaming - suffix and prefix 
qui{
renvars  r11*, suffix(r11)
renvars  r11*, predrop(3)
renvars  r12*, suffix(r12)
renvars  r12*, predrop(3)
renvars  r13*, suffix(r13)
renvars  r13*, predrop(3)
renvars  r10*, suffix(r10)
renvars  r10*, predrop(3)
renvars  r1*, suffix(r1)
renvars  r1*, predrop(2)
renvars  r2*, suffix(r2)
renvars  r2*, predrop(2)
renvars  r3*, suffix(r3)
renvars  r3*, predrop(2)
renvars  r4*, suffix(r4)
renvars  r4*, predrop(2)
renvars  r5*, suffix(r5)
renvars  r5*, predrop(2)
renvars  r6*, suffix(r6)
renvars  r6*, predrop(2)
renvars  r7*, suffix(r7)
renvars  r7*, predrop(2)
renvars  r8*, suffix(r8)
renvars  r8*, predrop(2)
renvars  r9*, suffix(r9)
renvars  r9*, predrop(2)

renvars  s11*, suffix(s11)
renvars  s11*, predrop(3)
renvars  s12*, suffix(s12)
renvars  s12*, predrop(3)
renvars  s13*, suffix(s13)
renvars  s13*, predrop(3)
renvars  s10*, suffix(s10)
renvars  s10*, predrop(3)
renvars  s1*, suffix(s1)
renvars  s1*, predrop(2)
renvars  s2*, suffix(s2)
renvars  s2*, predrop(2)
renvars  s3*, suffix(s3)
renvars  s3*, predrop(2)
renvars  s4*, suffix(s4)
renvars  s4*, predrop(2)
renvars  s5*, suffix(s5)
renvars  s5*, predrop(2)
renvars  s6*, suffix(s6)
renvars  s6*, predrop(2)
renvars  s7*, suffix(s7)
renvars  s7*, predrop(2)
renvars  s8*, suffix(s8)
renvars  s8*, predrop(2)
renvars  s9*, suffix(s9)
renvars  s9*, predrop(2)

renvars  h11*, suffix(h11)
renvars  h11*, predrop(3)
renvars  h12*, suffix(h12)
renvars  h12*, predrop(3)
renvars  h13*, suffix(h13)
renvars  h13*, predrop(3)
renvars  h10*, suffix(h10)
renvars  h10*, predrop(3)
renvars  h1*, suffix(h1)
renvars  h1*, predrop(2)
renvars  h2*, suffix(h2)
renvars  h2*, predrop(2)
renvars  h3*, suffix(h3)
renvars  h3*, predrop(2)
renvars  h4*, suffix(h4)
renvars  h4*, predrop(2)
renvars  h5*, suffix(h5)
renvars  h5*, predrop(2)
renvars  h6*, suffix(h6)
renvars  h6*, predrop(2)
renvars  h7*, suffix(h7)
renvars  h7*, predrop(2)
renvars  h8*, suffix(h8)
renvars  h8*, predrop(2)
renvars  h9*, suffix(h9)
renvars  h9*, predrop(2)
}

*********************************
***KEEP THE VARIABLES WE NEED ***
*********************************
#delimit ;
global varkeep_demographics iwstatr* iwstats*   iwendmr* iwendms* // 1=alive, 4=NR, alive, 5=NR, died this wave, 6=NR, died prev wave, 7=dropped from sample; months from last interview to death; interview end month
							wtcrnhr* wtcrnhs* // RwWTCRNH is the person-level weight and the nursing home weight combined. From Wave 1 to 4, there is no nursing home weight so RwWTCRNH equals RwWTRESP.
							ragender gender* // respondent gender, spouse gender ; 1=male, 2=female
                            cpl*             // whether couple household = married, partnered or 2 respondents in house (1=yes, 0=no)
							mstatr*          // respondent marital status 
										// .	missing
										// 1	Married
										// 2	Married, spouse absent
										// 3	Partnered
										// 4	Separated
										// 5	Divorced
										// 6   Separated/Divorced
										// 7   Widowed
										// 8   Never Married
							agey_m*          // ages at midpoint between beginning and end of interview reference period
							wtrespr* wtresps* wthhr* //person level analysis weight and hh weight
							radyear radmonth dyears*   //death year and month
							raedyrs edyrss* raedegrm edegrms* // years of education and highest degree
							hhresh*  //number of people living in the HH
							raracem racem* rahispan hispan*  // race and hispanic status, repsondent and spouse
							racohbyr*   // the birth cohort of respondent and spouse :Those with birth years before 1924 are set to the AHEAD cohort,
                                         // 1924-1930 are set to the CODA cohort, 1931-1941 are set to the HRS cohort, 1942-1947 are set to the WB cohort,
                                         // 1948-1953 are set to the EBB cohort, and 1954-1959 are set to the MBB cohort. If birth year is missing, then RACOHBYR
                                         // is missing. If birth year is after 1959, then RABCOHBYR is set to zero. 
								childh* livbror* livbros* livsisr* livsiss* livsibr* livsibs* //kids and family members		 
										 
										 ;

global varkeep_health shltr* shlts* cesdr* cesds* diabr* cancrr* lungr* heartr* strokr* psychr* arthrr*;	// self-reported health. 1=excellent, 5=poor; also, the score for mental health CESD

global varkeep_ADL 
							walkrr* walkrs* walkrhr* walkrhs* //walking across room - wheather has difficulty and wheather gets helpt with.
							dressr* dresss* dresshr* dresshs* 
							bathr* baths* bathhr* bathhs*
							eatr* eats* eathr* eaths*
							bedr* beds* bedhr* bedhs*
							toiltr* toilts* toilthr* toilths* 
							  // for all of these, use wave 3 onwards because waves 1 and 2 not consistently coded
							  // for difficulty vars : 0=no, 1=yes, 2=can't do, 9=don't do. 
							  // for help vars: 0=no, 1=yes
							
							walk1r* walk1s*  //walking 1 bloch
							sitr* sits*   // sitting 2 hrs
							chairr* chairs* //getting up
							clim1r* clim1s* // 1 flight stairs
							stoopr* stoops*
							liftr* lifts*   // 10 lbs
							armsr* armss*    // lift arms up 
							dimer* dimes*  // pick up dime
							walksr* walkss*  //walking several block
							   // 0=no, 1=yes, 2=can't do, 9=don't do.
							   adlar* adlas*  // summary measure for some difficulty with 5 ADL's (sum) : bath, dress, eat, bed, walk.
							alzher* demenr* memryr*  ; // memory related problems 
							
global varkeep_expectations pnhm5yr*	pnhm5ys* beq100r* beq100s* beq500r* beq500s* beqanyr* beqanys*;						
							
global varkeep_IADL  phoner* phones*
                     moneyr* moneys*
					 medsr* medss*
					 shopr* shops*
					 mealsr* mealss* ;
					 		  // 0=no, 1=yes, 2=can't do, 9=don't do.		 				 					 					       
							 
global varkeep_health_exp oopmdr* oopmds* ; // OOP ME last 2 yrs from wave 3 onwards;
                                           // In Wave 3A, 4 and 5, the components are 
										    //(1) hospital and nursing home costs; 
											//(2)doctor, dentist, and outpatient surgery costs; 
											//(3)average monthly prescription drug costs; and 
											//(4)home health care and special facilities or services costs. 
										   //Beginning in Wave 6, the components are 
										    //(1) hospital costs; 
											//(2) nursing home costs; 
											//(3)doctor visits costs; 
											//(4)dentist costs; 
											//(5)outpatient surgery costs; 
											//(6)average monthly prescription drug costs; 
											//(7) home health care and 
											//(8) special facilities cost.
					//totmbr* totmbir* ;  // total medical expenditures waves 3-6 inclusive, braketed and imputed.
global varkeep_insurance govmd* govmr* hiltc* tyltc*; //has medicaid, medicare, LT insurance, type of LT care ins 
							// 0=no, 1=yes, ; type: 1=NH only, 2=in Home care only, 3=both,  7=other

global varkeep_NH   nrshomr* nrshoms* // NH home (any nights), prev 2 yrs, except wave 1, prev 12 months.
					nrsnitr* nrsnits* // NH nights, prev 2 yrs, except wave 1, prev 12 months
					nhmdayr* nhmdays* // NH days since prev interview / move 
					homcar*           // If received home health care in prev 2 yrs, (12 months) (0=no, 1=yes)
					nhmlivr* nhmlivs* // If live in NH at time of interview.
					nhmlivh*;  // 0 = none reside in a nursing home; 1 = one of a couple resides in a nursing home; 2 = both in a couple reside in a nursing home; 3 = the household is a single-Respondent who resides in a nursing home
					
global varkeep_income ipen*  isret* iothr* itoth* igxfr*  issdi* iearn*  icaph*   // SS income for resp and spouse. from wave 3 onwards, this is income in the last year. Includes, own + spouse + widow benefits 	; ogxfr is income (R and S separately) from other gov. transfers (veteran benefits, food stamps, welfare)	; issdi is the sum of DI and SSI income	; capital income		
						jyearsr* jyearss* lbrfr* lbrfs* wgihrr* wgihrs* ; // also include # yrs worked total
						 
global varkeep_assets //arlesh* ; // arles = net value of other real estate (nominal dollars) - HH level; 					
                      atranh*  // net value of vehicles, hh level. 
					  absnsh*  // net value of business
					  aothrh* // otehr assets
					  atranh* // cars
					  airah*   // net value of IRA, Keogh accounts
					  //astckh*  // net value of stocks, mutual funds and inv trusts
					  //achckh*  // net value of checking, saving, or money market accounts
					  //acdh*    // CD, gov savings bonds and T-bills
					  //abondh*  // bonds and bond funds ? - do these include the other bonds?
					  //aothrh*  // all other savings
					  //adebth*  // value of other debt
					  ahoush*  // value of primary residence
					  atothh*  // net value of primary residence
					  anethbh* // net value of secondary residence
					  atotfh*  // net value of non-housing wealth
					  atotbh*  // total wealth - including secondary residence
					  atotnh*  // total non-housing wealth
					  atotfch* ; // change in non-house fin wealth

keep hhid  pn ID iwendyr* // HH_ID, person number, ID combining HH_ID and person number to uniquely identify individuals 
							// interview end year
	 $varkeep_demographics  $varkeep_health $varkeep_ADL $varkeep_IADL $varkeep_expectations
	 $varkeep_health_exp $varkeep_insurance $varkeep_NH $varkeep_income $varkeep_assets;
#delimit cr


order ID hhid pn gender* 



**********FOR RESHAPING ************

#delimit ;
global demographics iwstatr iwstats  iwendmr iwendms gender genders cplh mstatr agey_mr agey_ms wtrespr wtresps wthhr  dyears edyrss edegrms hhresh racohbyrr racohbyrs wtcrnhr wtcrnhs  racems  hispans childh livbror livbros livsisr livsiss livsibr livsibs ;
global health shltr shlts cesdr cesds diabr cancrr lungr heartr strokr psychr arthrr;	
global ADL 
							walkrr walkrs walkrhr walkrhs //walking across room - wheather has difficulty and wheather gets helpt with.
							dressr dresss dresshr dresshs 
							bathr baths bathhr bathhs
							eatr eats eathr eaths
							bedr beds bedhr bedhs
							toiltr toilts toilthr toilths 
							
							walk1r walk1s  //walking 1 bloch
							sitr sits   // sitting 2 hrs
							chairr chairs //getting up
							clim1r clim1s // 1 flight stairs
							stoopr stoops
							liftr lifts   // 10 lbs
							armsr armss // lift arms up
							dimer dimes   // pick up dime
							walksr walkss //walk several blocks	
							adlar adlas // summary # ADLs
							alzher demenr memryr ; // memory problems
global IADL  phoner phones
                     moneyr moneys
					 medsr medss
					 shopr shops
					 mealsr mealss ;		 				 					 					       
global expectations  pnhm5yr	pnhm5ys beq100r beq100s beq500r beq500s beqanyr beqanys;							 
global health_exp oopmdr oopmds ; //totmbr totmbir

global insurance govmdr govmds  govmrr govmrs hiltcr  hiltcs tyltcr tyltcs ; //has medicaid, medicare, LT insurance, type of LT care ins 
							
global NH   nrshomr nrshoms // NH home (any nights), prev 2 yrs, except wave 1, prev 12 months.
					nrsnitr nrsnits // NH nights, prev 2 yrs, except wave 1, prev 12 months
					nhmdayr nhmdays // NH days since prev interview / move 
					homcarr  homcars         // If received home health care in prev 2 yrs, (12 months) (0=no, 1=yes)
					nhmlivr nhmlivs // if live in NH at time of interview
					nhmlivh; // HH nursing home status.
					
global inc isretr isrets ipenr ipens ipenar ipenas iothrh itoth igxfrr igxfrs issdir issdis iearnr iearns icaph jyearsr jyearss lbrfr lbrfs wgihrr wgihrs;	

global assets atranh atothh anethbh atotfh	atotbh atotnh atotfch airah aothrh  absnsh ahoush;		

#delimit cr

drop phoners2 medsrs2 moneyrs2 mealsrs2 shoprs2  mealsrr2 shoprr2 phonerr2 medsrr2 moneyrr2

reshape long iwendyr  $demographics $health $ADL $IADL $expectations  $health_exp $insurance $NH $inc $assets, i(ID) j(wave)



* Keep only wave 3 and after 
keep if wave>=3

* RENAMING 

drop gender
rename ragender gender
rename genders gender_s

* RACE 
qui{
rename raracem race
rename racems race_s
rename rahispan hispanic
rename hispans hispanic_s
replace race=4 if race==3
replace race=3 if hispanic==1
label define l_race 1 "White or Other" 2 "Black" 3 "Hispanic" 
replace race=1 if race==4
label values race l_race

replace race_s=4 if race_s==3
replace race_s=3 if hispanic_s==1
replace race_s=1 if race_s==4
label values race_s l_race
}


***** MARITAL STATUS 
qui{
rename mstatr married
* MARRIED / NOT-MARRIED / WIDOWED
gen marital_status=1 if married==1 | married==2 | married==3 //married or partnered
replace marital_status=2 if married==4 | married==5 | married==6 | married==8 //separated, divorced, never married (single)
replace marital_status=3 if married==7 // widowed
replace marital_status=3  if marital_status==1 & (iwstats==5 | iwstats==6)

* MARRIED / NOT-MARRIED
gen marital_status_indic1=0 if marital_status==2 | marital_status==3 // single
replace marital_status_indic1=1 if marital_status==1 // married

* MARRIED OR WIDOWED / NOT-MARRIED
gen marital_status_indic2=0 if marital_status==1 | marital_status==3
replace marital_status_indic2=1 if marital_status==2

* MARRIED OR SINGLE / WIDOWED
gen marital_status_indic3=0 if marital_status==1 | marital_status==2
replace marital_status_indic3=1 if marital_status==3

label define mstatus 1 "Married" 2 "Single" 3 "Widowed"
label value marital_status mstatus
}



* AGE, AGE GROUPS, AND AGE DIFFERENCE SPOUSES
qui{

rename agey_mr age
rename agey_ms age_s
* variable with constant of relative age of spouse
* age of husband minus wife
gen age_diff = age-age_s  if gender==1
replace age_diff = age_s-age  if gender==2
tab age_diff
recode age_diff (-46/0 = 3) (1/8  = 2) (9/44 = 1) , gen(age_diff_group)  // 3= women older. 
* now create a constant within each ID - to find the relative age for widows later
bysort ID: egen age_diff_constant=max(age_diff_group)
label var age_diff_constant "Age Difference (husb-wife)"

* AGE GROUPS 
         gen age1=	52	if age>=	50	& age<	55
		replace age1=	57	if age>=	55	& age<	60
		replace age1=	62	if age>=	60	& age<	65
		replace age1=	67	if age>=	65	& age<	70
		replace age1=	72	if age>=	70	& age<	75
		replace age1=	77	if age>=	75	& age<	80
		replace age1=	82	if age>=	80	& age<	85
		replace age1=	87	if age>=	85	& age<	90
		replace age1=	92	if age>=	90	& age<	95
		replace age1=	97	if age>=	95	& age<	100
		replace age1=	102	if age>=	100	& age<	105
		replace age1=	107	if age>=	105	& age<	110

		
		
gen age2=	52	if age>=	51	& age<	55
		replace age2=	56	if age>=	55	& age<	59
		replace age2=	60	if age>=	59	& age<	63
		replace age2=	64	if age>=	63	& age<	67
		replace age2=	68	if age>=	67	& age<	71
		replace age2=	72	if age>=	71	& age<	75
		replace age2=	76	if age>=	75	& age<	79
		replace age2=	80	if age>=	79	& age<	83
		replace age2=	84	if age>=	83	& age<	87
		replace age2=	88	if age>=	87	& age<	91
		replace age2=	92	if age>=	91	& age<	95
		replace age2=	97	if age>=	95	& age<	100
		replace age2=	102	if age>=	100	& age<	105
		replace age2=	107	if age>=	105	& age<	110

gen age_HH= age if marital_status_indic1==0 // single
replace age_HH= age if marital_status_indic1==1 & gender==1 
replace age_HH= age_s if marital_status_indic1==1 & gender==2 
label variable age "Age"		
}

* KIDS AND SIBLINGS
replace childh=10 if childh>10 & childh!=. // cap at 10
replace livsisr=5 if livsisr>5 & livsisr!=. // cap at 5 sisters
replace livbror=5 if livbror>5 & livbror!=. // cap at 5 brothers


* expectations
rename pnhm5yr expect_NH
rename pnhm5ys expect_NH_s
tab expect_NH
recode expect_NH (0 = 0) (1/10  = 1) (11/24 = 2) (25/50 = 3) (51/100 = 4), gen(expect_NH_cat)  
tab expect_NH_cat

*medicaid
rename govmdr medicaid
replace medicaid=. if medicaid!=0 & medicaid!=1
rename govmds medicaid_s
replace medicaid_s=. if medicaid_s!=0 & medicaid_s!=1
label variable medicaid "Has Medicaid"


* YEARS WORKED (ENTIRE LIFE)
rename jyearsr work_yrs_r
rename jyearss work_yrs_s
label var work_yrs_r "Yrs worked"
label var work_yrs_s "Yrs worked"


*EDUCATION
qui{
gen college=0 if raedyrs<16 | raedyrs==0
replace  college=1 if 	raedyrs==16 | 	raedyrs==17
gen college_s=0 if edyrss<16 | edyrss==0
replace college_s=1 if edyrss==16 | edyrss==17
bysort ID: egen college_s_constant=max(college_s)
gen college_husband=college_s_constant if gender==2 // for women, 
gen college_HH=college
replace college_HH=college_husband if college_husband!=.
label var college_HH "College HH"
label var college "College"
label var college_s "College spouse"
gen college_some=0 if raedyrs<13 | raedyrs==0
replace  college_some=1 if raedyrs==13 | 	raedyrs==14 | 	raedyrs==15 | raedyrs==16 | 	raedyrs==17

gen college_some_s=0 if edyrss<13 | edyrss==0
replace college_some_s=1 if edyrss==13 | edyrss==14 | edyrss==15 | edyrss==16 | edyrss==17
bysort ID: egen college_some_s_constant=max(college_some_s)
gen college_some_husband=college_some_s_constant if gender==2 

gen college_some_HH=college_some
replace college_some_HH=college_some_husband if college_some_husband!=.
tab college_some
gen educ_3cat = 1 if college_some==0 
replace educ_3cat = 2 if college_some==1
replace educ_3cat = 3 if college==1
gen educ_3cat_s = 1 if college_some_s==0 
replace educ_3cat_s = 2 if college_some_s==1
replace educ_3cat_s = 3 if college_s==1
label define l_educ3 1 "HS or Less" 2 "Some College" 3 "College" 
label values educ_3cat  l_educ3
label values educ_3cat_s  l_educ3
label var educ_3cat "Education"
label var educ_3cat_s "Education spouse"
label var college_some_HH "Some College HH"
label var college_some "Some College"
label var college_some_s "Some College spouse"
}


***** HEALTH and ADLs*****
qui{
rename shltr health
rename shlts health_s
rename cesdr health_mental
rename cesds health_mental_s
* HEALTH STATUS - GOOD AND BAD
gen health_status = 0 if health==4 | health==5 // bad
replace health_status = 1 if health==1 | health==2 | health==3 // good 
gen health_status_s = 0 if health_s==4 | health_s==5 // bad
replace health_status_s = 1 if health_s==1 | health_s==2 | health_s==3 // good 

* LTC states - based on "getting help with..."

****************** health based on difficulties with... *****************

* alternative way of constructing the LTC state - has difficulty with 3+ ADL's rather than receiving help with 3+ ADLs. 
* R or S "has difficutly with..."

gen ADL1=1 if  walkrr==1 | walkrr==2 |  walkrr==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL2=1 if  dressr==1 | dressr==2 |  dressr==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL3=1 if   bathr==1 | bathr==2 |  bathr==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL4=1 if   eatr==1 | eatr==2 |  eatr==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL5=1 if   bedr==1 | bedr==2 |  bedr==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL6=1 if   toiltr==1 | toiltr==2 |  toiltr==9 // "yes, difficulty," or can't do" and "don't do"
replace ADL1=0 if  walkrr==0
replace ADL2=0 if  dressr==0
replace ADL3=0 if  bathr==0
replace ADL4=0 if  eatr==0
replace ADL5=0 if  bedr==0
replace ADL6=0 if  toiltr==0


gen ADL1_s=1 if  walkrs==1 | walkrs==2 |  walkrs==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL2_s=1 if  dresss==1 | dresss==2 |  dresss==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL3_s=1 if   baths==1 | baths==2 |  baths==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL4_s=1 if   eats==1 | eats==2 |  eats==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL5_s=1 if   beds==1 | beds==2 |  beds==9 // "yes, difficulty," or can't do" and "don't do"
gen ADL6_s=1 if   toilts==1 | toilts==2 |  toilts==9 // "yes, difficulty," or can't do" and "don't do"
replace ADL1_s=0 if  walkrs==0
replace ADL2_s=0 if  dresss==0
replace ADL3_s=0 if  baths==0
replace ADL4_s=0 if  eats==0
replace ADL5_s=0 if  beds==0
replace ADL6_s=0 if  toilts==0



gen ADL_combo_diff=0 if wave>=3 
replace ADL_combo_diff=1 if ADL1==1 & wave>=3
replace ADL_combo_diff=ADL_combo_diff*10 +2   if ADL2==1 & wave>=3
replace ADL_combo_diff=ADL_combo_diff*10 +3  if ADL3==1 & wave>=3
replace ADL_combo_diff=ADL_combo_diff*10 +4  if ADL4==1 & wave>=3
replace ADL_combo_diff=ADL_combo_diff*10 +5  if ADL5==1 & wave>=3
replace ADL_combo_diff=ADL_combo_diff*10 +6  if ADL6==1 & wave>=3

gen ADL_combo_diff_s=0 if wave>=3 
replace ADL_combo_diff_s=1 if ADL1_s==1 & wave>=3
replace ADL_combo_diff_s=ADL_combo_diff_s*10 +2   if ADL2_s==1 & wave>=3
replace ADL_combo_diff_s=ADL_combo_diff_s*10 +3  if ADL3_s==1 & wave>=3
replace ADL_combo_diff_s=ADL_combo_diff_s*10 +4  if ADL4_s==1 & wave>=3
replace ADL_combo_diff_s=ADL_combo_diff_s*10 +5  if ADL5_s==1 & wave>=3
replace ADL_combo_diff_s=ADL_combo_diff_s*10 +6  if ADL6_s==1 & wave>=3

drop ADL1 ADL2 ADL3 ADL4 ADL5 ADL6
drop ADL1_s ADL2_s ADL3_s ADL4_s ADL5_s ADL6_s

qui{
	
**** below I construct a variable ADL_combo that captures how many and which ADLs the respondent has. 
* R or S "gets help with..."
rename walkrhr ADL1
rename dresshr ADL2
rename bathhr ADL3
rename eathr ADL4
rename bedhr ADL5
rename toilthr ADL6

* "can't do" answer - these people are counted as having that ADL.
replace ADL1=1 if  walkrr==2 |  walkrr==9 // "can't do" and "don't do"
replace ADL2=1 if  dressr==2 |  dressr==9 // "can't do" and "don't do"
replace ADL3=1 if   bathr==2 |  bathr==9 // "can't do" and "don't do"
replace ADL4=1 if   eatr==2 |  eatr==9 // "can't do" and "don't do"
replace ADL5=1 if   bedr==2 |  bedr==9 // "can't do" and "don't do"
replace ADL6=1 if   toiltr==2 |  toiltr==9 // "can't do" and "don't do"
* and be careful that answers "can't do" and "don't do" might be in the previous question - i.e., do you have difficulty with...?


rename walkrhs ADL1_s
rename dresshs ADL2_s
rename bathhs ADL3_s
rename eaths ADL4_s
rename bedhs ADL5_s
rename toilths ADL6_s

replace ADL1_s=1 if  walkrs==2 |  walkrs==9 // "can't do" and "don't do"
replace ADL2_s=1 if  dresss==2 |  dresss==9 // "can't do" and "don't do"
replace ADL3_s=1 if   baths==2 |  baths==9 // "can't do" and "don't do"
replace ADL4_s=1 if   eats==2 |  eats==9 // "can't do" and "don't do"
replace ADL5_s=1 if   beds==2 |  beds==9 // "can't do" and "don't do"
replace ADL6_s=1 if   toilts==2 |  toilts==9 // "can't do" and "don't do"


gen ADL_combo_help=0 if wave>=3 
replace ADL_combo_help=1 if ADL1==1 & wave>=3
replace ADL_combo_help=ADL_combo_help*10 +2   if ADL2==1 & wave>=3
replace ADL_combo_help=ADL_combo_help*10 +3  if ADL3==1 & wave>=3
replace ADL_combo_help=ADL_combo_help*10 +4  if ADL4==1 & wave>=3
replace ADL_combo_help=ADL_combo_help*10 +5  if ADL5==1 & wave>=3
replace ADL_combo_help=ADL_combo_help*10 +6  if ADL6==1 & wave>=3

gen ADL_combo_help_s=0 if wave>=3 
replace ADL_combo_help_s=1 if ADL1_s==1 & wave>=3
replace ADL_combo_help_s=ADL_combo_help_s*10 +2   if ADL2_s==1 & wave>=3
replace ADL_combo_help_s=ADL_combo_help_s*10 +3  if ADL3_s==1 & wave>=3
replace ADL_combo_help_s=ADL_combo_help_s*10 +4  if ADL4_s==1 & wave>=3
replace ADL_combo_help_s=ADL_combo_help_s*10 +5  if ADL5_s==1 & wave>=3
replace ADL_combo_help_s=ADL_combo_help_s*10 +6  if ADL6_s==1 & wave>=3

***** IADLs ******
* number of IADLs where max is 5:  phone money meds shop meals

rename phoner IADL1
rename moneyr IADL2
rename medsr IADL3
rename shopr IADL4
rename mealsr IADL5

rename phones IADL1_s
rename moneys IADL2_s
rename medss IADL3_s
rename shops IADL4_s
rename mealss IADL5_s

foreach var of varlist IADL* {
replace `var' = 1 if `var'==2 | `var'==9
}

gen IADL_combo=0 if wave>=3 
replace IADL_combo=1 if IADL1==1 & wave>=3
replace IADL_combo=IADL_combo*10 +2   if IADL2==1 & wave>=3
replace IADL_combo=IADL_combo*10 +3  if IADL3==1 & wave>=3
replace IADL_combo=IADL_combo*10 +4  if IADL4==1 & wave>=3
replace IADL_combo=IADL_combo*10 +5  if IADL5==1 & wave>=3

gen IADL_combo_s=0 if wave>=3 
replace IADL_combo_s=1 if IADL1_s==1 & wave>=3
replace IADL_combo_s=IADL_combo_s*10 +2   if IADL2_s==1 & wave>=3
replace IADL_combo_s=IADL_combo_s*10 +3  if IADL3_s==1 & wave>=3
replace IADL_combo_s=IADL_combo_s*10 +4  if IADL4_s==1 & wave>=3
replace IADL_combo_s=IADL_combo_s*10 +5  if IADL5_s==1 & wave>=3
}		


egen ADL_total=rowtotal(ADL1 ADL2 ADL3 ADL4 ADL5 ADL6) //gets help with how many adls
label variable ADL_total "ADLs (max=6)"

egen IADL_total=rowtotal(IADL1 IADL2 IADL3 IADL4 IADL5 ) //gets help with how many adls
label variable IADL_total "IADLs (max=5)"

}



* NH status 
qui{
gen nursing_home_100=0 if nrshomr==0 | (nrshomr==1 & nrsnitr<100 )  
replace nursing_home_100=1 if nrshomr==1 & nrsnitr>=100 & nrsnitr!=. 

gen nursing_home_100_s=0 if nrshoms==0 | (nrshoms==1 & nrsnits<100 )
replace nursing_home_100_s=1 if  nrshoms==1 & nrsnits>=100 & nrsnits!=.
label variable  nhmlivr "Lives in NH"
label variable nursing_home_100 "Nursing Home (>100 days/past 2 yrs)"
}


* CPI ADJUST 
qui{
gen year=.
replace year=1992 if wave==1
replace year=1994 if wave==2
replace year=1996 if wave==3
replace year=1998 if wave==4
replace year=2000 if wave==5
replace year=2002 if wave==6
replace year=2004 if wave==7
replace year=2006 if wave==8
replace year=2008 if wave==9
replace year=2010 if wave==10
replace year=2012 if wave==11
replace year=2014 if wave==12
replace year=2016 if wave==13

* This is the CPI for year -1. *for example, for year 1992, the CPI here is for 1991
* I do this because many variables expressed in dollars are those from previous years. 
*                - Social Security
*                - OOP expenditures are for the previous 2 years combined (use last year's CPI). 
gen	CPI=	136.2	if 	year==	1992
replace	CPI=	144.5	if	year==	1994
replace	CPI=	152.4	if	year==	1996
replace	CPI=	160.5	if	year==	1998
replace	CPI=	166.6	if	year==	2000
replace	CPI=	177.1	if	year==	2002
replace	CPI=	184	if	year==	2004
replace	CPI=	195.3	if	year==	2006
replace	CPI=	207.342	if	year==	2008
replace	CPI=	214.537	if	year==	2010
replace	CPI=	224.939	if	year==	2012
replace	CPI=	232.957	if	year==	2014
replace	CPI=	237.017	if	year==	2016

* Some variables are for the current year 
*                 - assets
gen	CPI_c=	140.3	if 	year==	1992
replace	CPI_c=	148.2	if	year==	1994
replace	CPI_c=	156.9	if	year==	1996
replace	CPI_c=	163	   if	year==	1998
replace	CPI_c=	172.2	if	year==	2000
replace	CPI_c=	179.9	if	year==	2002
replace	CPI_c=	188.9	if	year==	2004
replace	CPI_c=	201.6	if	year==	2006
replace	CPI_c=	215.303	if	year==	2008
replace	CPI_c=	218.056	if	year==	2010
replace	CPI_c=	229.594	if	year==	2012
replace	CPI_c=	236.736	if	year==	2014
replace	CPI_c=	240.007	if	year==	2016

* CPI adjust to 2010 dollars
foreach var of varlist oopmdr oopmds ipenr ipenar ipens ipenas isretr isrets iothrh itoth igxfrr igxfrs issdir issdis iearnr iearns icaph wgihrr wgihrs{
replace `var'=`var'/CPI*218.056
}

foreach var of varlist atranh atothh anethbh atotfh	atotbh atotnh atotfch airah aothrh atranh absnsh ahoush {
replace `var'=`var'/CPI_c*218.056
}
*
}



qui{
/*
!jtype= 1 for single FEM, col=1   1=coll, 2=non-coll
!jtype= 2 for single FEM, col=2
!jtype= 3 for single MALE, col=1 
!jtype= 4 for single MALE, col=2
!jtype= 5 for married man , col=1 ,  wife= young  
!jtype= 6 for married man , col=1 ,  wife= av 
!jtype= 7 for married man , col=1 ,  wife= old 
!jtype= 8 for married man , col=2 ,  wife= young 
!jtype= 9 for married man , col=2 ,  wife= av
!jtype= 10 for married man , col=2 , wife= old  
*/
* this is the current status
gen jtype_new =1 if marital_status_indic1==0 & college_HH==1 & gender==2 
replace jtype_new =2 if marital_status_indic1==0 & college_HH==0 & gender==2 
replace jtype_new =3 if marital_status_indic1==0 & college_HH==1 & gender==1 
replace jtype_new =4 if marital_status_indic1==0 & college_HH==0 & gender==1 
replace jtype_new =5 if marital_status_indic1==1 & college_HH==1 & gender==1 & age_s<(age-7) 
replace jtype_new =6 if marital_status_indic1==1 & college_HH==1 & gender==1 & age_s<age  & age_s>(age-8) 
replace jtype_new =7 if marital_status_indic1==1 & college_HH==1 & gender==1 & age_s>=age 
replace jtype_new =8 if marital_status_indic1==1 & college_HH==0 & gender==1 & age_s<(age-7) 
replace jtype_new =9 if marital_status_indic1==1 & college_HH==0 & gender==1 & age_s<age  & age_s>(age-8) 
replace jtype_new =10 if marital_status_indic1==1 & college_HH==0 & gender==1 & age_s>=age 
* add the women too
replace jtype_new =11 if marital_status_indic1==1 & college_HH==1 & gender==2  & age<(age_s-7)
replace jtype_new =12 if marital_status_indic1==1 & college_HH==1 & gender==2 & age<age_s  & age>(age_s-8)
replace jtype_new =13 if marital_status_indic1==1 & college_HH==1 & gender==2 & age>=age_s
replace jtype_new =14 if marital_status_indic1==1 & college_HH==0 & gender==2  & age<(age_s-7)
replace jtype_new =15 if marital_status_indic1==1 & college_HH==0 & gender==2 & age<age_s  & age>(age_s-8) 
replace jtype_new =16 if marital_status_indic1==1 & college_HH==0 & gender==2 & age>=age_s

label variable jtype_new "Gender, education and age difference"
label define demog 1 "Single F, Coll" 2 "Single F, no Coll" 3 "Single M, Coll" 4 "Single M, no Coll" 5 "Married M, Coll, W young" 6 "Married M, Coll, W med" 7 "Married M, Coll, W old" 8 "Married M, no Coll, W young" 9 "Married M, no Coll, W med" 10 "Married M, no Coll, W old" 11 "Married F, Coll, W young" 12 "Married F, Coll, W med" 13 "Married F, Coll, W old" 14 "Married F, no Coll, W young" 15 "Married F, no Coll, W med" 16 "Married F, no Coll, W old"

label values jtype_new demog 

gen jtype =1 if marital_status_indic1==0 & college_HH==1 & gender==2 
replace jtype =2 if marital_status_indic1==0 & college_HH==0 & gender==2 
replace jtype =3 if marital_status_indic1==0 & college_HH==1 & gender==1 
replace jtype =4 if marital_status_indic1==0 & college_HH==0 & gender==1 
replace jtype =5 if (marital_status_indic1==1 | marital_status==3)  & college_HH==1 & gender==1 & age_diff_constant==1
replace jtype =6 if (marital_status_indic1==1 | marital_status==3) & college_HH==1 & gender==1 & age_diff_constant==2
replace jtype =7 if (marital_status_indic1==1 | marital_status==3) & college_HH==1 & gender==1 & age_diff_constant==3
replace jtype =8 if (marital_status_indic1==1 | marital_status==3) & college_HH==0 & gender==1 & age_diff_constant==1 
replace jtype =9 if (marital_status_indic1==1 | marital_status==3) & college_HH==0 & gender==1 & age_diff_constant==2 
replace jtype =10 if (marital_status_indic1==1 | marital_status==3) & college_HH==0 & gender==1 & age_diff_constant==3
* add the women too
replace jtype =11 if (marital_status_indic1==1 | marital_status==3) & college_HH==1 & gender==2  & age_diff_constant==1
replace jtype =12 if (marital_status_indic1==1 | marital_status==3) & college_HH==1 & gender==2 & age_diff_constant==2
replace jtype =13 if (marital_status_indic1==1 | marital_status==3) & college_HH==1 & gender==2 & age_diff_constant==3
replace jtype =14 if (marital_status_indic1==1 | marital_status==3) & college_HH==0 & gender==2  & age_diff_constant==1
replace jtype =15 if (marital_status_indic1==1 | marital_status==3) & college_HH==0 & gender==2 & age_diff_constant==2 
replace jtype =16 if (marital_status_indic1==1 | marital_status==3) & college_HH==0 & gender==2 & age_diff_constant==3




* generate type based on marital status, education, and age of married partners relative to one another.
gen type= 1 if marital_status_indic1==0 & college==0
replace type=2 if marital_status_indic1==0 & college==1 

replace type=3 if marital_status_indic1==1 & ((gender==1 & college==0 & age_s<(age-7) ) | (gender==2 & college_s==0 & age<(age_s-7) )) //married, non-col head, young wife
replace type=4 if marital_status_indic1==1 & ((gender==1 & college==0 & age_s<age  & age_s>(age-8)) | (gender==2 & college_s==0 & age<age_s  & age>(age_s-8)  ))  //married, non-col head, med
replace type=5 if marital_status_indic1==1 & ((gender==1 & college==0 & age_s>=age ) | (gender==2 & college_s==0 & age>=age_s ))   //married, non-col head, old

replace type=6 if marital_status_indic1==1 & ((gender==1 & college==1 & age_s<(age-7) ) | (gender==2 & college_s==1 & age<(age_s-7) )) //col head, young
replace type=7 if marital_status_indic1==1 & ((gender==1 & college==1 & age_s<age  & age_s>(age-8)) | (gender==2 & college_s==1 & age<age_s  & age>(age_s-8)  )) //col head, med
replace type=8 if marital_status_indic1==1 & ((gender==1 & college==1 & age_s>=age ) | (gender==2 & college_s==1 & age>=age_s ))  //col head, old

gen type_sex_marital = 1 if gender==1 & marital_status_indic1==0 // single male
replace type_sex_marital = 2 if gender==2 & marital_status_indic1==0  // single female
replace type_sex_marital = 3 if type==3 | type==6 // married, wife young
replace type_sex_marital = 4 if type==4 | type==7 // married, wife med
replace type_sex_marital = 5 if type==5 | type==8 // married, wife old
}

order wave ID hhid pn gender gender_s  age age_s married  cplh   iwstatr iwstats radyear radmonth dyears wtrespr wtresps  
sort ID wave



* MERGE WITH THE HRS FILE THAT HAVE INFO ON HELP RECEIVED
merge ID wave using "${data_raw}\Help_variables_HRS.dta"
tab _merge
sort _merge
drop if _merge==2
drop _merge
sort ID year


**** HEALTH 
qui{
gen hlppror=1 if nhmlivr==1 
gen hlppros=1 if nhmlivs==1

* health

gen HEALTH_MODEL= 1 if health_status == 1 //good
replace HEALTH_MODEL= 2 if health_status ==0 //bad
replace HEALTH_MODEL= 3 if ( (ADL_combo_help>0 &  ADL_combo_help!=. )  | (IADL_combo>0 &  IADL_combo!=. ))  // gets help with at least one ADL or has difficulty with IADL
replace HEALTH_MODEL= 4 if ( (ADL_combo_help>0 &  ADL_combo_help!=. )  | (IADL_combo>0 &  IADL_combo!=. ))  & (IC_total_90_r==1 |  hlppror==1 )

tab HEALTH_MODEL if age>65
tab HEALTH_MODEL if IC_total_90_r ==.m & age>65 

gen HEALTH_MODEL_s= 1 if health_status_s == 1 //good
replace HEALTH_MODEL_s= 2 if health_status_s ==0 //bad
replace HEALTH_MODEL_s= 3 if ( (ADL_combo_help_s>0 &  ADL_combo_help_s!=. )  | (IADL_combo_s>0 &  IADL_combo_s!=. ))  // gets help with at least one ADL or has difficulty with IADL
replace HEALTH_MODEL_s= 4 if ( (ADL_combo_help_s>0 &  ADL_combo_help_s!=. )  | (IADL_combo_s>0 &  IADL_combo_s!=. ))  & (IC_total_90_s==1 | hlppros==1)

rename  health health_ORIG
rename  health_s health_s_ORIG
rename  HEALTH_MODEL health
rename  HEALTH_MODEL_s health_s

* SPOUSE DISABLED
gen Spouse_disabled = 0 if health_s==1 | health_s==2
replace Spouse_disabled = 1 if health_s==3 | health_s==4


* health of head of HH - male for married 
gen health_HH =  health if marital_status_indic1==0 // single
replace health_HH = health if marital_status_indic1==1 & gender==1 // married male's health
replace health_HH = health_s if marital_status_indic1==1 & gender==2 // the husband's health

gen LTC_indic=0 
replace LTC_indic=1 if health==4 
label variable  LTC_indic "In LTC State"
}



* medicaid at HH level
qui{
gen medicaid_HH=medicaid if jtype_new<5 // singles
replace medicaid_HH= 0 if medicaid==0 & medicaid_s==0 & jtype_new>=5 & jtype_new!=. // married, neither has medicaid
replace medicaid_HH= 1 if (medicaid==1 | medicaid_s==1) & jtype_new>=5 & jtype_new!=. & medicaid_s!=. & medicaid!=.
label variable medicaid_HH "HH has Medicaid"
}


* wealth and wealth groups 
qui{
rename atotbh  total_wealth // this is total wealth including primary and secondary residence

* wealth groups
gen wealth_group = 1 if  total_wealth<60000
replace wealth_group = 2 if total_wealth>60000 & total_wealth<110000
replace wealth_group = 3 if total_wealth>110000 & total_wealth<160000
replace wealth_group = 4 if total_wealth>160000 & total_wealth<300000
replace wealth_group = 5 if total_wealth>300000 & total_wealth!=.

label variable wealth_group "Wealth group (thousands)"
label define wg 1 "Less than 60" 2 "60-109" 3 "110-159" 4 "160-299"  5 "300 or more"
label values wealth_group wg

label variable  total_wealth "Total Wealth"
}


* treat those that are already widowed at 65 the same as singles. 
gen widow_65 = 1 if marital_status==3 & (age ==64 | age==65 | age ==66)
bysort ID: egen widow_65_max = max(widow_65)
replace marital_status=2 if marital_status==3 & widow_65_max==1

* COHORTS
label define cohort_label 1 "AHEAD" 2 "CODA" 3 "HRS" 4 "Warbabies" 5 "Early Babyboomers" 6 "Mid Baby Boomers"
label variable racohbyr "Cohort"
label values racohbyr cohort_label

* REPLACE GENDER = 0 IF MALE
qui{
replace gender=0 if gender == 1 // males are 0 now
replace gender=1 if gender==2
label variable gender "Female"

label define gdr 0 "Male" 1 "Female" 
label values gender gdr
}



replace atothh=0 if atothh<0
gen H_Wealth_indic =1 if atothh>30000 & atothh!=.
replace H_Wealth_indic =0 if atothh<=30000

label variable H_Wealth_indic "Housing Wealth > 30K"



drop if age_HH==.
drop if college_HH==.

drop if year==2016 // we don't have care variables for the last wave
keep if year>=2000

drop if health==.
drop if marital_status_indic1==1 & health_s==.

drop if medicaid==.
drop if medicaid_s==. & marital_status_indic1==1
drop if marital_status_indic1==.

drop if  iwstatr==. // missing interview status
sort ID wave

save "${data}/HRS_Analysis.dta", replace



